package com.example.weblending.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.weblending.domain.File;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.mapper.FileResultMapper;
import com.example.weblending.service.FileService;
import com.example.weblending.mapper.FileMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 *
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File>
implements FileService{
    @Resource
    private FileResultMapper fileResultMapper;
    @Override
    public Page<File> getListById(long page, long limit, String id) {
        QueryWrapper<File> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        wrapper.eq("uid",id);
        wrapper.orderByDesc("uid");
        Page<File> pageParam = new Page<>(page,limit);
        baseMapper.selectPage(pageParam,wrapper);
        pageParam.getRecords();
        //map返回
        return pageParam;
    }

    @Override
    public boolean remove(int id) {
        File file=getById(id);
        if(file!=null){
            removeById(id);
            fileResultMapper.deleteById(id);
            try{
                java.io.File files = new java.io.File(file.getPath());
                if(files.exists()){
                    files.delete();
                }
                files = new java.io.File(file.getCleanpath());
                if(files.exists()){
                    files.delete();
                }
                files = new java.io.File(file.getFinishpath());
                if(files.exists()){
                    files.delete();
                }
            }catch (Exception e){}

            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean clean(int id, String uid) {
        return false;
    }

    @Override
    public String getCleanPath(String uid, int id) {
        File file=getById(id);
        if(file==null){
            throw new GlobalException(404,"该文件没有找到");
        }
        if(file.getStatus()==0){
            throw new GlobalException(400,"请先清洗文件");
        }
        if(file.getUid()!=Integer.parseInt(uid)){
            throw new GlobalException(403,"您无权操作该文件");
        }
        return file.getCleanpath();
    }

    @Override
    public String getAnalyzePath(String uid, int id) {
        File file=getById(id);
        if(file==null){
            throw new GlobalException(404,"该文件没有找到");
        }
        if(file.getStatus()!=2){
            throw new GlobalException(400,"请先分析文件");
        }
        if(file.getUid()!=Integer.parseInt(uid)){
            throw new GlobalException(403,"您无权操作该文件");
        }
        return file.getFinishpath();
    }
}




